package com.onegene.pdf.component.paint;

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.element.Image;
import com.onegene.pdf.component.report.colon.ColonColor;

import java.net.URL;

/**
 * @author: laoliangliang
 * @description:
 * @create: 2020/5/20 8:34
 **/
public class ColonPainting extends AbstractPainting {
    private boolean isChangAi;

    public ColonPainting(PdfDocument pdf, PdfFont font) {
        super(pdf, font);
    }

    public ColonPainting(PdfDocument pdf, PdfFont font, boolean isChangAi) {
        super(pdf, font);
        this.isChangAi = isChangAi;
    }

    public void drawHeader(float yOff) {
        URL resource = ColonPainting.class.getClassLoader().getResource("image/colon/logo.png");
        Image backImage = new Image(ImageDataFactory.create(resource));
        float width = 50;
        float height = 23;
        backImage.setWidth(width);
        backImage.setHeight(height);
        PdfPage page = pdf.getPage(pdf.getNumberOfPages());
        Rectangle pageSize = page.getPageSize();
        Rectangle rectangle = new Rectangle(
                53,
                pageSize.getTop() - 40 + yOff,
                width,
                height);

        Canvas canvas = new Canvas(page, rectangle);
        canvas.add(backImage).flush();
        canvas.close();

        pdfCanvas.saveState();
        pdfCanvas.setStrokeColor(ColonColor.getThemeColor());
        pdfCanvas.setLineWidth(0.5f);
        float y = pageSize.getTop() - 43 + yOff;
        float left = 55;
        pdfCanvas.moveTo(left, y)
                .lineTo(pageSize.getWidth() - left, y).stroke();

        float yOffset = pageSize.getTop();
        if (isChangAi) {
            pdfCanvas.beginText()
                    .setFontAndSize(font, 11)
                    .setFillColor(ColonColor.getThemeColor())
                    .moveText(pageSize.getWidth() - 165, yOffset - 40 + yOff);
            pdfCanvas.showText("早发现·早诊断·早治疗");
        } else {
            pdfCanvas.beginText()
                    .setFontAndSize(font, 11)
                    .setFillColor(ColonColor.getThemeColor())
                    .moveText(pageSize.getWidth() - 170, yOffset - 40 + yOff);
            pdfCanvas.showText("控制致病菌·胃癌早预防");
        }
        pdfCanvas.endText();

        pdfCanvas.restoreState();
    }

    public void drawHeaderReverse(float yOff) {
        URL resource = ColonPainting.class.getClassLoader().getResource("image/colon/logo.png");
        Image backImage = new Image(ImageDataFactory.create(resource));
        float width = 50;
        float height = 23;
        backImage.setWidth(width);
        backImage.setHeight(height);
        PdfPage page = pdf.getPage(pdf.getNumberOfPages());
        Rectangle pageSize = page.getPageSize();
        Rectangle rectangle;
        if (isChangAi) {
            rectangle = new Rectangle(
                    pageSize.getWidth() - 106,
                    pageSize.getTop() - 40 + yOff,
                    width,
                    height);
        }else{
            rectangle = new Rectangle(
                    pageSize.getWidth() - 106,
                    pageSize.getTop() - 40 + yOff,
                    width,
                    height);
        }

        Canvas canvas = new Canvas(page, rectangle);
        canvas.add(backImage).flush();
        canvas.close();

        pdfCanvas.saveState();
        pdfCanvas.setStrokeColor(ColonColor.getThemeColor());
        pdfCanvas.setLineWidth(0.5f);
        float y = pageSize.getTop() - 43 + yOff;
        float left = 55;
        pdfCanvas.moveTo(left, y)
                .lineTo(pageSize.getWidth() - left, y).stroke();

        float yOffset = pageSize.getTop();
        if (isChangAi) {
            pdfCanvas.beginText()
                    .setFontAndSize(font, 11)
                    .setFillColor(ColonColor.getThemeColor())
                    .moveText(54, yOffset - 40 + yOff);
            pdfCanvas.showText("早发现·早诊断·早治疗");
        } else {
            pdfCanvas.beginText()
                    .setFontAndSize(font, 11)
                    .setFillColor(ColonColor.getThemeColor())
                    .moveText(54, yOffset - 40 + yOff);
            pdfCanvas.showText("控制致病菌·胃癌早预防");
        }
        pdfCanvas.endText();

        pdfCanvas.restoreState();
    }


    public void close() {
        pdfCanvas.release();
    }

}